import { http } from "@/utils/http";

export const useUserAPI = () => {
  /**
   * 使用用户名和密码登录
   */
  const postLoginByPsw = (username: string, password: string): any => {
    return http.post(`auth/login?uname=${username}&upassword=${password}`);
  };
  /**
   * 退出登录
   */
  const postLogout = (): any => {
    return http.post("/user/logout");
  };
  /**
   * 注册用户
   */
  const postRegister = (username: string, password: string): any => {
    return http.post(
      `/user/registerSimple?uname=${username}&upassword=${password}`
    );
  };
  /**
   * 申请成为音乐人
   * @param userId 用户ID
   */
  const postApplyMusician = (userId: string): any => {
    return http.post(`/user/applyForMusician?userId=${userId}`);
  };
  /**
   * 上传用户头像
   * @param uid 用户ID
   * @param avatarUrl 头像URL地址
   */
  const putUpdateAvatar = (uid: string, avatarUrl: string): any => {
    const params = new URLSearchParams();
    params.append("uid", uid);
    params.append("avatarUrl", avatarUrl);
    return http.put("/user/changeAvatarByUid", params);
  };
  /**
   * 更新用户信息
   * @param userInfo 用户信息对象
   */
  const postUpdateUserInfo = (userInfo: {
    uid: string;
    uname: string;
    email: string;
    phone: string;
    sex: string;
    birthday: string;
    avatarUrl: string;
    description: string;
  }): any => {
    return http.post("/user/updateUserInfo", userInfo);
  };

  /**
   * 获取音乐人的统计数据
   * @param uid 用户ID
   */
  const getMusicianStats = (uid: string): any => {
    return http.get(`/musician/stats?uid=${uid}`);
  };

  /**
   * 获取音乐人的所有音乐
   * @param uid 用户ID
   */
  const getMusicianSongs = (singerName: string): any => {
    return http.get(`/music/queryBySinger?singerName=${singerName}`);
  };

  /**
   * 上传音乐
   * @param musicData 音乐信息对象
   */
  const postUploadSong = (musicData: {
    musicName: string;
    musicAuthor: string;
    musicUrl: string;
    musicCoverUrl: string;
    lyric?: string;
  }): any => {
    return http.post("/music/addMusic", musicData);
  };

  /**
   * 下架音乐
   * @param musicId 音乐ID
   */
  const deleteSong = (musicId: string): any => {
    return http.get(`/music/underMusic?musicId=${musicId}`);
  };

  return {
    postLoginByPsw,
    postRegister,
    postLogout,
    postApplyMusician,
    putUpdateAvatar,
    postUpdateUserInfo,
    getMusicianStats,
    getMusicianSongs,
    postUploadSong,
    deleteSong,
  };
};
